//frequency anaisis tools
class freq_anal {//it always hurts the first time but that just means I love you. 

	//distribution of leter frequency in english, element 0 = a
	//source: http://pages.central.edu/emp/LintonT/classes/spring01/cryptography/letterfreq.html
	public static int[] freq_table  ={8167,1492,2782,4253,12702,2228,2015,6094,6966,153,772,4025,2406,6749,7507,1929,95,5987,6327,9056,2758,978,2360,150,1974,74};

	//testing
	public static void main(String[] args) {
		
		//should be closer to 1 for english phrases
		double r_value = r(args[0]);

		System.out.println(r_value);
	}

	//tmakes a letters only historigram from a string, not case sencitive
	public static int[] historigram(String str) {

	int[] hist = new int[26]; 
	
	//not case sencitive
	str = str.toLowerCase();

	//str into char array 
	char[] c = str.toCharArray();

		//for each element in the new string array
		for(int i = 0; i < c.length; i++) {
		
			//if the element is a letter
			if(Character.isLetter(c[i])) {

				//add 1 to the corisponding element in the alpha_array
				hist[(int)c[i] - 97] += 1;
			}
		}	
        
		return hist;
	}

	//pearson correlation relating a string to freq_table, should return close to 1 for english phrases
	public static double r(String str) {
		int[] hist = historigram(str);
		return pearson.r(hist, freq_table);
	}
}

